package com.gitee.luosl.cola.match.pattern;


import java.util.function.Function;
import java.util.function.Predicate;

public interface Pattern<T, R> {

    R apply(T value);

    boolean isDefinedAt(T value);

    static <T, R> Pattern<T, R> create(Function<T, R> apply, Predicate<T> predicate) {
        return new Pattern<T, R>() {
            @Override
            public R apply(T value) {
                return apply.apply(value);
            }

            @Override
            public boolean isDefinedAt(T value) {
                return predicate.test(value);
            }
        };
    }

}
